from PySide6.QtWidgets import QApplication, QMenu, QMainWindow, QWidget, QSystemTrayIcon
from PySide6.QtGui import QIcon
import ballWin
import configWin
import helpWin
import myUtils


class TrayIcon(QSystemTrayIcon):
    def __init__(self, parent=None):
        super().__init__(parent)
        # 设置任务栏图标
        self.setIcon(QIcon(myUtils.resource_path('yu.ico')))
        # 创建菜单项
        self.menu = QMenu()
        self.menu.addAction('打开/隐藏 悬浮窗口', lambda: self.open_ball_window())
        self.menu.addAction('设置', lambda: self.open_config_window())
        self.menu.addAction('使用说明', lambda: self.open_help_window())
        self.menu.addAction('退出', lambda: self.exit())
        self.setContextMenu(self.menu)
        self.activated.connect(self.main_fun)
        # 默认打开悬浮窗口
        self.open_ball_window()

    def main_fun(self, reason):
        if reason == QSystemTrayIcon.ActivationReason.Trigger:
            # print("左键点击事件")
            self.open_ball_window()
        elif reason == QSystemTrayIcon.ActivationReason.Context:
            print("右键点击事件")

    def open_ball_window(self):
        if not hasattr(self, 'main_window') or self.main_window.isHidden():
            self.main_window = ballWin.BallWindow(self)
            self.main_window.show()
        else:
            self.main_window.close()

    def open_config_window(self):
        if not hasattr(self, 'new_window') or self.new_window.isHidden():
            self.new_window = configWin.ConfigWindow(self)
        else:
            self.new_window.close()
        self.new_window.show()

    def open_help_window(self):
        if not hasattr(self, 'help_window') or self.help_window.isHidden():
            self.help_window = helpWin.HelpWindow(self)
        else:
            self.help_window.close()
        self.help_window.show()

    def yingyong(self):
        # 当configWin点击应用时，会调用此方法，让main重新加载配置文件
        if hasattr(self, 'main_window') and not self.main_window.isHidden():
            self.main_window.loadConfig(False)

    def exit(self):
        QApplication.quit()


if __name__ == '__main__':
    app = QApplication([])
    # 当时最后一个可视窗口退出后，程序是否退出，设置为false
    app.setQuitOnLastWindowClosed(False)
    tray_icon = TrayIcon()
    tray_icon.show()
    app.exec()
